﻿using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BeiJingQuestion3
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonRightDouble_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string hexString = RawHexText.Text.Replace(" ", "");
                long num = Convert.ToInt64(hexString, 16);
                double result = BitConverter.Int64BitsToDouble(num);
                NumberText.Text = result.ToString(CultureInfo.InvariantCulture);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    $"转换错误: {ex.Message}",
                    "错误",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                );
            }
        }

        private void ButtonRightFloat_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string hexString = RawHexText.Text.Replace(" ", "");
                uint num = Convert.ToUInt32(hexString, 16);
                float result = BitConverter.ToSingle(BitConverter.GetBytes(num), 0);
                NumberText.Text = result.ToString(CultureInfo.InvariantCulture);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    $"转换错误: {ex.Message}",
                    "错误",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                );
            }
        }

        private void ButtonLeftDouble_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (
                    double.TryParse(
                        NumberText.Text,
                        NumberStyles.Any,
                        CultureInfo.InvariantCulture,
                        out double number
                    )
                )
                {
                    long bits = BitConverter.DoubleToInt64Bits(number);
                    string hex = bits.ToString("X16");
                    RawHexText.Text = hex;
                }
                else
                {
                    MessageBox.Show(
                        "请输入有效的Double数值.",
                        "错误",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error
                    );
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    $"转换错误: {ex.Message}",
                    "错误",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                );
            }
        }

        private void ButtonLeftFloat_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (
                    float.TryParse(
                        NumberText.Text,
                        NumberStyles.Any,
                        CultureInfo.InvariantCulture,
                        out float number
                    )
                )
                {
                    byte[] bytes = BitConverter.GetBytes(number);
                    uint num = BitConverter.ToUInt32(bytes, 0);
                    string hex = num.ToString("X8"); // Format as 8-character hex
                    RawHexText.Text = hex;
                }
                else
                {
                    MessageBox.Show(
                        "请输入有效的Float数值.",
                        "错误",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error
                    );
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    $"转换错误: {ex.Message}",
                    "错误",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                );
            }
        }

        private void ButtonRightInt32_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string hexString = RawHexText.Text.Replace(" ", "");
                int result = Convert.ToInt32(hexString, 16);
                NumberText.Text = result.ToString(CultureInfo.InvariantCulture);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    $"转换错误: {ex.Message}",
                    "错误",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                );
            }
        }

        private void ButtonRightInt64_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string hexString = RawHexText.Text.Replace(" ", "");
                long result = Convert.ToInt64(hexString, 16);
                NumberText.Text = result.ToString(CultureInfo.InvariantCulture);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    $"转换错误: {ex.Message}",
                    "错误",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                );
            }
        }

        private void ButtonLeftInt32_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (
                    int.TryParse(
                        NumberText.Text,
                        NumberStyles.Any,
                        CultureInfo.InvariantCulture,
                        out int number
                    )
                )
                {
                    string hex = number.ToString("X8"); // Format as 8-character hex
                    RawHexText.Text = hex;
                }
                else
                {
                    MessageBox.Show(
                        "请输入有效的Int32数值.",
                        "错误",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error
                    );
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    $"转换错误: {ex.Message}",
                    "错误",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                );
            }
        }

        private void ButtonLeftInt64_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (
                    long.TryParse(
                        NumberText.Text,
                        NumberStyles.Any,
                        CultureInfo.InvariantCulture,
                        out long number
                    )
                )
                {
                    string hex = number.ToString("X16"); // Format as 16-character hex
                    RawHexText.Text = hex;
                }
                else
                {
                    MessageBox.Show(
                        "请输入有效的Int64数值.",
                        "错误",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error
                    );
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    $"转换错误: {ex.Message}",
                    "错误",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                );
            }
        }
    }
}
